/*
 * Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
 */
package com.pitop.further.ctrl;


import com.alibaba.fastjson.JSON;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.tools.redis.RedisUtils;
import com.pitop.further.service.FurtherSVImpl;
import com.pitop.serial.entity.SerialNumber;
import com.pitop.test.entity.TestUnit;
import com.pitop.test.service.TestUnitSVImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 工厂管理 控制器
 *
 * @author pi-top
 */
@RestController
@RequestMapping("/further")
@Slf4j
@Api(value = "further接口控制器", description = "further接口控制器")
public class FurtherCtrl {

    @Resource
    protected RedisUtils redisUtils;
    @Autowired
    private FurtherSVImpl furtherSV;


    @Autowired
    private TestUnitSVImpl testUnitSV;


    /**
     * 查询SerialNumber一个详情信息
     *
     * @param barcode sn编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询SerialNumber一个详情信息", notes = "查询SerialNumber一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "barcode", value = "sn编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/barcode/{barcode}")
    @ResponseBody
    public BeanRet loadByBarcode(@PathVariable String barcode) {
        if (StringUtils.isEmpty(barcode)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        SerialNumber serialNumber = furtherSV.loadByBarcode(barcode);
        log.info(JSON.toJSONString(serialNumber));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, serialNumber);
    }


    /**
     * 查询SerialNumber一个详情信息
     *
     * @param serial serial
     * @return BeanRet
     */
    @ApiOperation(value = "查询SerialNumber一个详情信息", notes = "查询SerialNumber一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "serial", value = "serial", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/serial/{serial}")
    @ResponseBody
    public BeanRet loadBySerial(@PathVariable String serial) {
        if (StringUtils.isEmpty(serial)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        SerialNumber serialNumber = furtherSV.loadBySerial(serial);
        TestUnit testUnit = new TestUnit();
        if(serialNumber != null) {
            testUnit = testUnitSV.loadByCode(serialNumber.getTestUnitCode());
        }
        log.info(JSON.toJSONString(serialNumber));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, testUnit.getSn());
    }


}
